﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections.Specialized;

namespace LoginServerControl
{
    public class ServerControl : WebControl, IPostBackDataHandler, IPostBackEventHandler
    {

        public string UserName
        {
            get;
            set;
        }

        public string Password
        {
            get;
            set;
        }

        //add Css fle
        protected void AddCSSFile()
        {
            bool linkIncluded = false;
            foreach (Control c in Page.Header.Controls)
            {
                if (c.ID == "StyleOfServerControl")
                {
                    linkIncluded = true;
                }
            }
            if (!linkIncluded)
            {
                HtmlGenericControl csslink = new HtmlGenericControl("link");
                csslink.ID = "StyleOfServerControl";
                csslink.Attributes.Add("href",
                                                             Page.ClientScript.GetWebResourceUrl(this.GetType(), "LoginServerControl.Styles.StyleOfServerControl.css"));
                csslink.Attributes.Add("type", "text/css");
                csslink.Attributes.Add("rel", "stylesheet");
                Page.Header.Controls.Add(csslink);

            }
        }

        //add script to page
        protected void AddScriptFile()
        {
            if (!Page.ClientScript.IsClientScriptIncludeRegistered("ScriptOfServerControl"))
            {
                // When pre-rendering, add in external JavaScript file
                Page.ClientScript.RegisterClientScriptInclude("ScriptOfServerControl",
                                         Page.ClientScript.GetWebResourceUrl(this.GetType(), "LoginServerControl.Scripts.ScriptOfServerControl.js"));
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            AddCSSFile();
            AddScriptFile();

            base.OnPreRender(e);
        }

        // at least one control had to have name UniqueID
        protected override void RenderContents(HtmlTextWriter writer)
        {

            writer.AddAttribute(HtmlTextWriterAttribute.Class, "serverControl");
            writer.RenderBeginTag(HtmlTextWriterTag.Fieldset);

            writer.RenderBeginTag(HtmlTextWriterTag.Legend);
            writer.Write("ServerControl");
            writer.RenderEndTag();

            //first rows
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "rowOfServerControl");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            //user name (input)
            writer.AddAttribute(HtmlTextWriterAttribute.Type, "Text");
            writer.AddAttribute(HtmlTextWriterAttribute.Value, "");
            writer.AddAttribute(HtmlTextWriterAttribute.Id, UniqueID + "UserName");
            writer.AddAttribute(HtmlTextWriterAttribute.Name, UniqueID + "UserName");
            writer.RenderBeginTag(HtmlTextWriterTag.Input);

            writer.AddAttribute(HtmlTextWriterAttribute.Width, "20px");
            writer.AddAttribute(HtmlTextWriterAttribute.Id, UniqueID + "lblUserNameValidate");
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "color: red;display: none;");
            writer.RenderBeginTag(HtmlTextWriterTag.Span);
            writer.Write("*");
            writer.RenderEndTag();

            writer.RenderBeginTag(HtmlTextWriterTag.Span);
            writer.Write("User Name");
            writer.RenderEndTag();


            writer.RenderEndTag();
            //end div first
            writer.RenderEndTag();

            //second rows
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "rowOfServerControl");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);

            //password(input)
            writer.AddAttribute(HtmlTextWriterAttribute.Type, "Password");
            writer.AddAttribute(HtmlTextWriterAttribute.Value, "");
            writer.AddAttribute(HtmlTextWriterAttribute.Id, UniqueID + "Password");
            writer.AddAttribute(HtmlTextWriterAttribute.Name, UniqueID + "Password");

            writer.RenderBeginTag(HtmlTextWriterTag.Input);

            writer.AddAttribute(HtmlTextWriterAttribute.Width, "20px");
            writer.AddAttribute(HtmlTextWriterAttribute.Id, UniqueID + "lblPasswordValidate");
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "color: red;display: none");
            writer.RenderBeginTag(HtmlTextWriterTag.Span);
            writer.Write("*");
            writer.RenderEndTag();


            writer.RenderBeginTag(HtmlTextWriterTag.Span);
            writer.Write("Password");
            writer.RenderEndTag();

            writer.RenderEndTag();

            //end div second
            writer.RenderEndTag();

            //third rows
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "rowOfServerControl");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);

            //submit(submit)
            writer.AddAttribute(HtmlTextWriterAttribute.Type, "submit");
            writer.AddAttribute(HtmlTextWriterAttribute.Value, "Submit");
            writer.AddAttribute(HtmlTextWriterAttribute.Id, UniqueID);
            writer.AddAttribute(HtmlTextWriterAttribute.Name, UniqueID);
            //  writer.AddAttribute(HtmlTextWriterAttribute.

            writer.AddAttribute(HtmlTextWriterAttribute.Class, "submitOfServerControl");
            writer.RenderBeginTag(HtmlTextWriterTag.Input);
            writer.RenderEndTag();

            //end div third
            writer.RenderEndTag();


            writer.RenderEndTag();
            base.RenderContents(writer);


        }
        public event EventHandler<ServerControlEventArgs> ButtonClicked;


        #region IPostBackDataHandler methods
        //all events in my server control
        public void RaisePostDataChangedEvent()
        {
            var Temp = ButtonClicked;
            if (Temp != null)
                Temp(this, new ServerControlEventArgs(Password, UserName));
        }

        //incoming form data
        public bool LoadPostData(string postDataKey, NameValueCollection postCollection)
        {
            UserName = postCollection[postDataKey + "UserName"];
            Password = postCollection[postDataKey + "Password"];

            Page.RegisterRequiresRaiseEvent(this);

            return false;
        }
        #endregion

        public void RaisePostBackEvent(string eventArgument)
        {
            RaisePostDataChangedEvent();
        }
    }
}
